package com.highveldsky.appengine.authplay.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;

@SuppressWarnings("serial")
public class PrintUserInfoAsTable extends TagSupport {
	
	private UserService userService;


	public void setUserService(UserService userService){
		this.userService = userService;
	}
	
	public int doStartTag() throws JspException {
		User user = userService.getCurrentUser();
		String authDomain = "";
		String email = "";
		String fedId = "";
		String nickname = "";
		String userId = "";
		if (user != null) {
			authDomain = user.getAuthDomain();
			email = user.getEmail();
			fedId = user.getFederatedIdentity();
			nickname = user.getNickname();
			userId = user.getUserId();
		}
		String result = "";
		result += "<div id='user-info-table'>";
		result += "<h2>User Info</h2>";
		result += "<p>This was generated by my custom tag:)</p>";
		result += "<table>";
		result += "<tr>";
		result += "<td>is user logged in?</td>";
		result += "<td>"+userService.isUserLoggedIn()+"</td>";
		result += "<tr>";
		result += "<tr>";
		result += "<td>is user admin?</td>";
		result += "<td>"+ (userService.isUserLoggedIn() ? userService.isUserAdmin() : "null") +"</td>";
		result += "<tr>";
		result += "<tr>";
		result += "<td>auth domain</td>";
		result += "<td>"+authDomain+"</td>";
		result += "<tr>";
		result += "<tr>";
		result += "<td>email</td>";
		result += "<td>"+email+"</td>";
		result += "<tr>";
		result += "<tr>";
		result += "<td>federation id</td>";
		result += "<td>"+fedId+"</td>";
		result += "<tr>";
		result += "<tr>";
		result += "<td>nickname</td>";
		result += "<td>"+nickname+"</td>";
		result += "<tr>";
		result += "<tr>";
		result += "<td>user id</td>";
		result += "<td>"+userId+"</td>";
		result += "<tr>";
		result += "</table>";
		result += "</div>";
		
		JspWriter out = pageContext.getOut();
		try {
			out.print(result);
		} catch (IOException e) {
			throw new JspException(e);
		}
		return EVAL_BODY_INCLUDE;
	}


	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}
}


